package com.caterina.resources;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;
import person.Person;

import budget.Request;


public class CModels {
	public static String getRequestModel(Request req,Person committer){
			String model=CModels.loadModel(CModels.REQUEST_MODEL);
			model=model.replace("@COMMITTER_NAME", committer.getName());
			model=model.replace("@COMMITTER_SURNAME", committer.getSurname());
			model=model.replace("@COMMITTER_PHONE_NUMBER", committer.getPhoneNumber());
			model=model.replace("@COMMITTER_MAIL", committer.getEmail());
			model=model.replace("@COMMITTER_FAX", committer.getFax());
			model=model.replace("@COMMITTER_ADDRESS", committer.getAddress());
			model=model.replace("@EVENT_NAME", req.getEvent().getEventName());
			model=model.replace("@EVENT_MEMO", req.getMemo());
			model=model.replace("@EVENT_PLACE", req.getEvent().getPlace());
			model=model.replace("@EVENT_TYPE", CWords.getWord(req.getEvent().getEventType().name()));
			model=model.replace("@EVENT_PREPARATION", CWords.getWord(req.getEvent().getPreparationType().name()));
			model=model.replace("@EVENT_LOCATION", req.getEvent().getPlace());
			model=model.replace("@EVENT_DATE", CWords.getFormattedDate(req.getEvent().getBeginDate()));
			model=model.replace("@REQUEST_DATE", CWords.getFormattedDate(req.getRequestDate()));		
			return model;
	}
	public static String getTransactionStatsModel(TreeMap<Integer,HashMap<String,Double>> stats){
			String model=CModels.loadModel(CModels.TRANSACTION_STATS_MODEL);
			model=model.replaceAll("@entryes", getEntriesModel(stats));
			return model;
	}
	private static String getEntriesModel(TreeMap<Integer,HashMap<String,Double>> stats){
		String s="";
		for(Integer year:stats.keySet()){
			s+=CModels.loadModel(CModels.TRANSACTION_STATS_ENTRY_MODEL);
			s=s.replaceAll("@CWordsSTATS_YEAR", CWords.getWord("STATS_YEAR"));
			s=s.replaceAll("@CWordsTRANSACTION_STATS_IN", CWords.getWord("TRANSACTION_STATS_IN"));
			s=s.replaceAll("@CWordsTRANSACTION_STATS_OUT", CWords.getWord("TRANSACTION_STATS_OUT"));
			s=s.replaceAll("@CWordsTRANSACTION_STATS_TOT", CWords.getWord("TRANSACTION_STATS_TOT"));
			s=s.replaceAll("@YEAR", Integer.toString(year));
			s=s.replaceAll("@YEAR", Integer.toString(year));
			s=s.replaceAll("@TRANSACTION_STATS_IN",Double.toString(stats.get(year).get("TRANSACTION_STATS_IN")));
			s=s.replaceAll("@TRANSACTION_STATS_OUT",Double.toString(stats.get(year).get("TRANSACTION_STATS_OUT")));
			s=s.replaceAll("@TRANSACTION_STATS_TOT",Double.toString(stats.get(year).get("TRANSACTION_STATS_TOT")));
		}
		return s;
	}
	public static String getRegisterMailModel(Person person,String username,String password){
		String loaded;
		loaded = loadModel(REGISTRATION_MAIL_MODEL);
		loaded=loaded.replaceAll("@PERSON_NAME", person.getName());
		loaded=loaded.replaceAll("@PERSON_SURNAME", person.getSurname());
		loaded=loaded.replaceAll("@USERNAME", username);
		loaded=loaded.replaceAll("@PASSWORD", password);
		loaded=loaded.replaceAll("@CATERINA_STYLE", getCaterinaStyle());
		return loaded;
	}
	public static String getLoadingPage(String text){
		if (text==null)text="";
		File fl=new File("graphics/loading.gif");
		if (fl.exists());String flPath="file:\\"+fl.getAbsolutePath();
		String model=CModels.loadModel(LOADING_MODEL);
		model=model.replaceAll("@CATERINA_STYLE", getCaterinaStyle());
		model=model.replaceAll("@LOADING_TEXT", text);
		model=model.replaceAll("@SPRITE_PATH", flPath);
		return model;
	}
	public static String getCaterinaStyle(){
		String style="<style type=\"text/css\">";
		style+=loadModel(CATERINA_STYLE);
		style+="</style>";
		return style;
	}
	
	private static String loadModel(String modelPath){
		try {
		Scanner sc = new Scanner(new File(modelPath));
		StringBuilder sb=new StringBuilder();
		while(sc.hasNextLine())sb.append(sc.nextLine());
		return sb.toString();
		} catch (FileNotFoundException e) {return "Errore durante il caricamento del modello";}
	}
	private static final String LOADING_MODEL="res/models/loading.html";
	private static final String REGISTRATION_MAIL_MODEL="res/models/registerMail.html";
	private static final String REQUEST_MODEL="res/models/EventRequest.html";
	private static final String CATERINA_STYLE="res/models/caterinaStyle.css";
	private static final String TRANSACTION_STATS_MODEL="res/models/transactionStats.html";
	private static final String TRANSACTION_STATS_ENTRY_MODEL="res/models/transactionStatsEntry.html";
}
